package algorithmTopics.stack;

import java.util.Stack;

/**
 * 最小堆栈
 *
 * @author 86155
 * @date 2025/07/20
 */
class MinStack {

    private Stack<Integer> stack;

    /**
     * 存储最小值
     */
    private Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        // 存储最小值
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.push(val);
        }
    }
    
    public void pop() {
        Integer val = stack.pop();
        if (minStack.peek().equals(val)) {
            minStack.pop();
        }
    }
    
    public int top() {
        return stack.peek();
    }

    /**
     * 获取栈里面最小值
     *
     * @return int
     */
    public int getMin() {
        return minStack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */